﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Liang.DataBase.Model.Entity;
using Liang.UI;
using Liang.DataBase.BLL;
using Liang.DataBase.Tools;

namespace Client
{
    public partial class FrmRoleEdit : Form
    {
        public sys_role role { get; set; }

        RoleBLL roleBLL = new RoleBLL();

        /// <summary>
        /// 所有APP
        /// </summary>
        IList<sys_app> allApps = null;

        public FrmRoleEdit()
        {
            InitializeComponent();
        }

        public FrmRoleEdit(sys_role role)
        {
            InitializeComponent();
            this.role = role;
        }

        private void FrmRoleEdit_Load(object sender, EventArgs e)
        {
            init();
        }

        public void init()
        {
            allApps = roleBLL.LoadAllApps();

            IList<sys_app> tmp = new List<sys_app>();

            if (role != null)
            {
                this.Text = "角色管理-修改";
                this.txtName.Text = role.name;
                this.txtName.ReadOnly = true;
                this.txtDes.Text = role.des;

                tmp = role.apps.ToList();
            }
            else
            {
                this.Text = "角色管理-新增";
            }

            foreach (sys_app itemApp in allApps)
            {
                ListViewItem lvItem = this.listView_App.Items.Add(itemApp.name);

                // 判断当前app是否已分配给了当前role
                var n = tmp.Where(q => q.appId.Equals(itemApp.appId));

                if (0 < n.Count())
                {
                    lvItem.Checked = true;
                }

                lvItem.Tag = itemApp;
            }
        }

        public bool validate()
        {
            if (string.IsNullOrEmpty(txtName.Text))
            {
                MessageBoxEx.Show(this, "请输入角色名称。");
                this.txtName.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txtDes.Text))
            {
                MessageBoxEx.Show(this, "请输入描述。");
                this.txtDes.Focus();
                return false;
            }

            if (role == null)
            {
                if (null != roleBLL.FindSingle(LambdaHelper.True<sys_role>().And(p => p.name.Equals(txtName.Text))))
                {
                    MessageBoxEx.Show(this, "组合名称重复，请重新输入。");
                    this.txtName.Focus();
                    return false;
                }
            }
            return true;
        }

        IList<sys_app> getApps()
        {
            IList<sys_app> roleApps = new List<sys_app>();

            foreach (ListViewItem lvItem in this.listView_App.Items)
            {
                if (lvItem.Checked)
                {
                    roleApps.Add((sys_app)lvItem.Tag);
                }
            }
            return roleApps;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (validate())
            {
                if (role == null)
                {
                    // 新增
                    role = new sys_role();
                    role.name = txtName.Text;
                    role.des = txtDes.Text;
                    role.apps = getApps();
                    roleBLL.Insert(role);
                }
                else
                {
                    // 编辑
                    role = roleBLL.FindByID(role.roleId);
                    role.name = txtName.Text;
                    role.des = txtDes.Text;
                    // 获取选中的appId
                    IList<sys_app> selectedApps = getApps();
                    var selectedAppIds = new HashSet<Guid>(selectedApps.Select(q => q.appId));

                    // 查询当前role包含的 app 的 appId
                    var roleAppIds = new HashSet<Guid>(role.apps.Select(q => q.appId));

                    // 
                    foreach (sys_app item in allApps)
                    {
                        if (selectedAppIds.Contains(item.appId))
                        {
                            if (!roleAppIds.Contains(item.appId))
                            {
                                role.apps.Add(item);
                            }
                        }
                        else
                        {
                            if (roleAppIds.Contains(item.appId))
                            {
                                role.apps.Remove(item);
                            }
                        }
                    }

                    roleBLL.Update(role, role.roleId);
                }

                DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
